<template>
  <div id="logIndex">
    <h1 class="con-right-title">
      登录日志
    </h1>
    <!--上部搜索条件组件-->
    <topSearch :form-inline="formInline" @toSearch="toSearch"></topSearch>
    <div class="table-conBtn">
      <el-button type="danger" icon="el-icon-delete" @click="cleanLog">
        清空
      </el-button>
      <el-button
        v-permission="'wh:system:loginLog:remove'"
        type="warning"
        plain
        @click="deleteLog"
      >
        批量删除
      </el-button>
      <el-button
        v-permission="'wh:system:loginLog:export'"
        type="warning"
        icon="el-icon-download"
        @click="exportData"
      >
        导出
      </el-button>
      <freshCom @searchList="searchList"></freshCom>
    </div>
    <!--下部table-->
    <el-table
      v-loading="loading"
      :data="tableData"
      stripe
      style="width: 100%"
      @selection-change="handleSelectionChange"
      max-height="490"
      ref="table"
    >
      <el-table-column
        type="selection"
        width="55"
        align="center"
      ></el-table-column>
      <el-table-column
        label="访问编号"
        prop="infoId"
        show-overflow-tooltip
        min-width="120"
      ></el-table-column>
      <el-table-column
        label="登录名称"
        prop="loginName"
        min-width="130"
      ></el-table-column>
      <el-table-column
        label="登录地址"
        prop="ipAddr"
        min-width="120"
      ></el-table-column>
      <el-table-column
        label="登录地点"
        prop="loginLocation"
        min-width="120"
      ></el-table-column>
      <el-table-column
        label="浏览器"
        prop="browser"
        min-width="120"
      ></el-table-column>
      <el-table-column
        label="操作系统"
        prop="os"
        min-width="110"
      ></el-table-column>
      <el-table-column label="登录状态" prop="status" min-width="120">
        <template slot-scope="scope">
          <span v-if="scope.row.status === 0" class="tag-span primary"
            >成功</span
          >
          <span v-if="scope.row.status === 1" class="tag-span danger"
            >失败</span
          >
        </template>
      </el-table-column>
      <el-table-column
        label="操作信息"
        prop="msg"
        min-width="110"
      ></el-table-column>
      <el-table-column
        label="登录时间"
        show-overflow-tooltip
        prop="loginTime"
        width="150"
      >
      </el-table-column>
    </el-table>
    <!--分页-全局组件-->
    <pageCom
      :searchParams="searchParams"
      :total="total"
      @searchList="searchList"
    ></pageCom>
  </div>
</template>

<script>
import topSearch from '@component/system/logManage/loginLog/topSearch' // 上部搜索条件
import { saveScrollMixins } from '@/mixins/saveScroll'
import {
  getSearchList,
  deleteSingleRequest,
  deleteMultRequest,
  clearRequest,
  downUrl
} from '@/api/system/loginLog'
export default {
  name: 'loginLog',
  msg: '日志管理',
  mixins: [saveScrollMixins],
  components: {
    topSearch
  },
  data() {
    return {
      loading: false,
      total: 0,
      tableData: [], // table内容
      searchParams: {
        pageNum: 1, // 当前页数
        pageSize: 10 // 页面显示条数
      },
      typeList: [], // 操作类型
      formInline: {
        // 搜索条件
        ipAddr: '', // 登录地址
        loginName: '', // 登录名称
        status: '', // 状态0正常,1停用
        startTime: '', // 开始时间
        endTime: '', // 结束时间
        createTime: []
      },
      multipleSelection: [],
      powerStr: 'monitor:logininfor:list', // 是否查询的标识
      canGetList: true, // 是否可以查询列表
      powerAry: [], // 可显示的权限
      canShowPage: false // 是否显示当前页面
    }
  },
  activated() {
    this.toTableScrollHeight()
  },
  mounted() {
    // this.getPower()
    this.searchList()
    this.$nextTick(() => {
      setTimeout(() => {
        this.setTableScrollHeight()
      }, 1000)
    })
  },
  methods: {
    /* 获取权限 */
    getPower() {
      let id = this.$route.query.mId
      this.getCurPageMenu(id).then((res) => {
        this.powerAry = res
        this.canShowPage = true
        if (!res.includes(this.powerStr)) {
          this.canGetList = false
          this.alertPower(this.powerStr)
        }
        this.searchList()
      })
    },
    /* 获取列表 */
    searchList() {
      if (!this.canGetList) return
      this.loading = true
      let obj = Object.assign({}, this.searchParams, this.formInline)
      delete obj.createTime
      let params = obj
      if (params.status === '') {
        delete params.status
      }
      getSearchList(
        params,
        (response) => {
          this.loading = false
          this.tableData = response.rows
          this.total = response.total
        },
        (response) => {
          this.loading = false
          this.$message({
            message: response.message,
            type: 'warning'
          })
        }
      )
    },
    /* table选择 */
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    // 删除日志
    deleteLog() {
      if (this.multipleSelection.length <= 0) {
        this.$message.warning('请至少选择一条日志！')
        return
      }
      this.$confirm(
        `确认删除选中的这${this.multipleSelection.length}条日志?`,
        '删除提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        let ids = []
        if (this.multipleSelection.length === 1) {
          ids[0] = this.multipleSelection[0].infoId
          this.delSingleItem(ids[0])
        } else {
          this.multipleSelection.forEach((item) => {
            ids.push(item.infoId)
          })
          this.delMultItem(ids)
        }
      })
    },
    // 删除单个日志
    delSingleItem(ids) {
      let url = `/login-logs/${ids}`
      deleteSingleRequest(
        ids,
        (response) => {
          this.searchList()
        },
        (response) => {
          this.$message({
            message: response.message,
            type: 'warning'
          })
        }
      )
    },
    // 删除多个日志
    delMultItem(ids) {
      let url = '/login-logs'
      let params = { ids }
      deleteMultRequest(
        params,
        (response) => {
          this.searchList()
        },
        (response) => {
          this.$message({
            message: response.message,
            type: 'warning'
          })
        }
      )
    },
    // 清空日志
    cleanLog() {
      this.$confirm('确定清空所有登录日志吗?', '清空提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        clearRequest(
          (response) => {
            this.searchList()
          },
          (response) => {
            this.$message({
              message: response.message,
              type: 'warning'
            })
          }
        )
      })
    },
    // 导出
    exportData() {
      this.$confirm('确定导出所有登录日志吗?', '导出提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          let url = downUrl()
          let params = Object.assign({}, this.searchParams, this.formInline)
          delete params.createTime
          if (params.status === '') {
            delete params.status
          }
          this.downFile('get', url, params, '登录日志')
        })
        .catch(() => {})
    },
    // 分页
    changePage(page) {
      this.searchParams.pageNum = page
      this.searchList()
    },
    /* 搜索 */
    toSearch() {
      this.searchParams.pageNum = 1
      this.searchList()
    }
  }
}
</script>

<style lang="less">
// #index {
// }
</style>
